package com.tanguy.user.db

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import com.tanguy.user.utlis.extend.loge
import org.jetbrains.anko.db.*

/**
 * Created by zhangyujiu on 2017/8/1.
 */
class MyDatabaseOpenHelper(context: Context) : ManagedSQLiteOpenHelper(context, "MingdeDB", null, 1) {
    companion object {
        private var instance: MyDatabaseOpenHelper? = null

        @Synchronized
        fun getInstance(context: Context): MyDatabaseOpenHelper {
            if (instance == null) {
                instance = MyDatabaseOpenHelper(context.applicationContext)
            }
            return instance!!
        }

    }

    override fun onCreate(db: SQLiteDatabase?) {
        loge()
        //地址历史记录表
        db?.createTable("AdHistory", true,
                "id" to INTEGER + PRIMARY_KEY,
                "name" to TEXT,
                "address" to TEXT,
                "latitude" to TEXT,
                "longitude" to TEXT,
                "adcode" to TEXT)
        //联系人历史记录表
        db?.createTable("Contact", true,
                "id" to INTEGER + PRIMARY_KEY,
                "name" to TEXT,
                "phone" to TEXT)
//        //航班历史记录表
//        db?.createTable("Flight", true,
//                "id" to INTEGER + PRIMARY_KEY,
//                "flightNumber" to TEXT)
    }

    override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
//        db?.dropTable("AdHistory", true)
        db?.dropTable("Contact", true)
//        db?.dropTable("Flight", true)
    }
}

val Context.database: MyDatabaseOpenHelper
    get() = MyDatabaseOpenHelper.getInstance(applicationContext)
